//
//  XLGetGiftRecordVC.m
//  XiangLove
//
//  Created by 李传坤 on 2018/7/31.
//  Copyright © 2018年 BAOMIHUA.COM. All rights reserved.
//

#import "XLGetGiftRecordVC.h"
#import "XLHTTPSessionManager.h"
#import "XLAccountManager.h"
#import "XLIncomeRecordCell.h"
#import <ReactiveCocoa.h>
#import "MJRefresh.h"
#import "BMHPromptView.h"
#import "XLMyFriendTableCell.h"
#import "XLGiftIncomeModel.h"
@interface XLGetGiftRecordVC ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView    *tableView;
@property (nonatomic,strong) NSMutableArray *modelArray;
@property (nonatomic,copy  ) NSString            *page;
@property (nonatomic,strong) UIButton       *charmBtn;
@property (nonatomic, strong) BMHPromptView      *promptView;

@end

@implementation XLGetGiftRecordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _page = @"1";
    // Do any additional setup after loading the view.
    self.fd_prefersNavigationBarHidden = YES;
    if (@available(iOS 11.0, *)) {
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self addTableView];
    [self requrstTalkedPeoplePage];
    
}
#pragma mark - getter
- (BMHPromptView *)promptView {
    if (_promptView == nil) {
        _promptView = [[BMHPromptView alloc] initWithImage:[UIImage imageNamed:@"pic_nothing_n"]];
        [_promptView.reTryButton setTitle:@"还没有收到礼物哦" forState:UIControlStateNormal];
        [self.view addSubview:_promptView];
        [_promptView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(200, 215));
            make.center.equalTo(self.view);
        }];
    }
    return _promptView;
}

-(void)requrstTalkedPeoplePage
{
    NSDictionary *dic = @{@"pageIndex":_page,
                          @"pageSize":@"16",
                          @"userId":[[XLAccountManager shareInstance]model].Id,
                          @"type":@"5"
                          };
    [[[XLHTTPSessionManager sharedManager]XLV2GET:@"/api/v2/Balance/GetUserReward" parameters:dic resultClass:[XLGiftIncomeModel class]] subscribeNext:^(id x) {
        NSLog(@"美女列表==%@",x);
        [MBProgressHUD hideHUDForView:self.view];
        self.tableView.hidden = NO;
        
        NSArray * listAry = x;
        if (![x isKindOfClass:[NSNull class]] && listAry.count > 0) {
            [_promptView removeFromSuperview];
            _promptView = nil;
            [self getJsonArray:listAry];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
        }else{
            if ([self.page isEqualToString:@"1"]) {
                self.promptView.hidden = NO;
                self.tableView.hidden = YES;
            }
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    } error:^(NSError *error) {
        NSLog(@"%@", error);
        if ([self.page isEqualToString:@"1"]) {
            self.promptView.hidden = NO;
            self.tableView.hidden = YES;
        }
        [MBProgressHUD hideHUDForView:self.view];
        if (self.modelArray.count == 0) {
        }
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }];
    
}

- (void)getJsonArray:(NSArray *)array {
    if ([array isKindOfClass:[NSNull class]] || array.count == 0 || array == nil) {
        
    } else {
        if ([self.page isEqualToString:@"1"]) {
            [self.modelArray removeAllObjects];
        } else {
            
        }
        [self.modelArray addObjectsFromArray:array];
    }
    [self.tableView reloadData];
}
#pragma mark - 下拉刷新
- (void)handelGCHeaderTableView {
    self.page = @"1";
    [self requrstTalkedPeoplePage];
    //[self reshOnlinePeoplesData];
    
}

#pragma mark - 上拉加载
- (void)handelGCFootTableView {
    self.page = [NSString stringWithFormat:@"%d", [self.page intValue] + 1];
    [self requrstTalkedPeoplePage];
}

- (NSMutableArray *)modelArray
{
    if (_modelArray == nil) {
        _modelArray = [NSMutableArray array];
    }
    return _modelArray;
}

-(void)addTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, XQScreenW, XQScreenH) style:UITableViewStylePlain];
    _tableView.rowHeight = 86;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.separatorColor = [UIColor groupTableViewBackgroundColor];
    [_tableView registerClass:[XLIncomeRecordCell class] forCellReuseIdentifier:NSStringFromClass([XLIncomeRecordCell class])];
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(handelGCHeaderTableView)];
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(handelGCFootTableView)];
    [self.view addSubview:_tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XLIncomeRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XLIncomeRecordCell class]) forIndexPath:indexPath];
    XLGiftIncomeModel  *model = [self.modelArray objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [XLDownloadImageTool downloadImage:model.Img place:nil imageView:cell.headerImageView];
    cell.tittleName.text = [NSString stringWithFormat:@"%@送%@ x%@",model.FromUser,model.GiftName,model.CountNum];
    cell.inforLabel.text = model.CreateTime;
    cell.Income.text =     [NSString stringWithFormat:@"+%@元",model.ChangeMoney];
    cell.rightBottomLabel.text = @"";
    
    return  cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
